package com.example.brvah_test;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView brvah_rv;
    private ArrayList<String> rv_data;
    private MyRVAdapter myRVAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        brvah_rv = findViewById(R.id.brvah_rv);
        myrv();
    }

    private void myrv() {
        brvah_rv.setLayoutManager(new LinearLayoutManager(this));
        initData();
//        initMultData();
    }

    private void initMultData() {
        ArrayList<Bean> list = new ArrayList<>();
        list.add(new Bean("我的", "16个", 1));
        list.add(new Bean("全部客户16个", "更新时间2010/01/01", 2));
        list.add(new Bean("龙图腾", "最近联系", 3));
        list.add(new Bean("测试人", "最近联系", 3));
        list.add(new Bean("全部客户16个", "更新时间2010/01/01", 2));
        list.add(new Bean("全部客户16个", "更新时间2010/01/01", 2));

        list.add(new Bean("欧阳震华", "最近联系", 3));
        list.add(new Bean("刘女士", "最近联系", 3));
        list.add(new Bean("我的", "16个", 1));
        list.add(new Bean("我的", "16个", 1));

        list.add(new Bean("张先生", "最近联系", 3));


//        LinearLayoutManager manager = new LinearLayoutManager(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemType = list.get(position).getItemType();
                if (itemType == 1) {
                    return 2;
                }
                if (itemType == 2) {
                    return 1;
                }
                if (itemType == 3) {
                    return 3;
                }
                return 1;
            }
        });
        brvah_rv.setLayoutManager(gridLayoutManager);

        //这里是你要适配的数据，多布局只需要一个参数就行了
        MyMultiAdapter apader = new MyMultiAdapter(list);
        brvah_rv.setAdapter(apader);
    }

    private void initData() {
        rv_data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            rv_data.add("测试数据" + i);
        }
        ArrayList<String> rv_rv_data = new ArrayList<>();
        rv_rv_data.add("我是内部数据");
        rv_rv_data.add("我是内部数据");
        myRVAdapter = new MyRVAdapter(this, R.layout.rv_item, rv_data, rv_rv_data);
        //在高版本studio上，brvah版本也升到高版本（3.0.11）
        brvah_rv.setAdapter(myRVAdapter);
        brvah_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            private int thisPosition = -1;

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    LinearLayoutManager layoutManager = (LinearLayoutManager) brvah_rv.getLayoutManager();
                    if (layoutManager != null) {
                        int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                        String s = rv_data.get(firstCompletelyVisibleItemPosition);
                        if (firstCompletelyVisibleItemPosition == thisPosition) return;
                        thisPosition = firstCompletelyVisibleItemPosition;
                        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}